package com.ruoyi.project.purchase.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.purchase.domain.RyDepotDetail;
import com.ruoyi.project.purchase.domain.pojo.DepotDetailBean;
import com.ruoyi.project.purchase.domain.pojo.DepotHeadItem;
import com.ruoyi.project.purchase.domain.pojo.ParamBean;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.purchase.domain.RyPurchaseOut;
import com.ruoyi.project.purchase.service.IRyPurchaseOutService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 项目物品出库Controller
 *
 * @author you
 * @date 2022-09-26
 */
@RestController
@RequestMapping("/purchase/ryPurchaseOut")
public class RyPurchaseOutController extends BaseController
{
    @Autowired
    private IRyPurchaseOutService ryPurchaseOutService;

    /**
     * 查询项目物品出库列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:ryPurchaseOut:list')")
    @GetMapping("/list")
    public TableDataInfo list(RyPurchaseOut ryPurchaseOut)
    {
        startPage();
        ryPurchaseOut.setType("出库");
        ryPurchaseOut.setSubType("项目物品");
        List<RyPurchaseOut> list = ryPurchaseOutService.selectRyPurchaseOutList(ryPurchaseOut);
        return getDataTable(list);
    }


    /**
     * --项目物品出库
     * 新增项目物品出库信息
     * @return
     * @throws Exception
     */
    @RequestMapping("/addDepotHeadAndDetail")
    public AjaxResult addDepotHeadAndDetail(@RequestBody DepotHeadItem depotHeadItem)
    {
//        System.out.println("depotHeadItem---->" + depotHeadItem);
        System.out.println("linkHeadId---->" + depotHeadItem.getLinkHeadId());
        return ryPurchaseOutService.addDepotHeadAndDetail(depotHeadItem);
    }

    /**
     * --项目物品出库
     * 根据主表修改单据子表信息
     */
    @RequestMapping("/editPurchaseOutDetail")
    public AjaxResult editPurchaseOutDetail(@RequestBody DepotHeadItem depotHeadItem){
        System.out.println("depotHeadItem---->" + depotHeadItem);
        return ryPurchaseOutService.editDepotDetail(depotHeadItem);
    }

    /**
     * --项目物品出库
     * 单条数据出库
     */
    @RequestMapping("/oneOutDetail")
    public AjaxResult oneOutDetail(@RequestBody RyDepotDetail ryDepotDetail){
        System.out.println("ryDepotDetail---->" + ryDepotDetail);
        return ryPurchaseOutService.oneOutDetail(ryDepotDetail);
    }

    /**
     * --项目物品出库
     * 获取单据主表详细信息
     */
    @GetMapping(value = "/selectOutDetailByDepotId/{id}")
    public AjaxResult selectOutDetailByDepotId(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ryPurchaseOutService.selectRyDepotDetailByDepotId(id));
    }


    /**
     * 根据条件查询项目明细数据
     */
    @GetMapping("/selectItemForOutByDepotId")
    public AjaxResult selectItemForOutByDepotId(ParamBean paramBean){
        //设置查询参数
        ParamBean param = new ParamBean();
        if(paramBean.getLinkHeadId() != null){
            param.setId(paramBean.getLinkHeadId());
        }
        if(!StringUtils.isBlank(paramBean.getInventoryName())){
            param.setInventoryName(paramBean.getInventoryName());
        }
        return AjaxResult.success(ryPurchaseOutService.selectItemForOutByDepotId(param));
    }




    /**
     * 导出采购出库列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:ryPurchaseOut:export')")
    @Log(title = "采购出库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RyPurchaseOut ryPurchaseOut)
    {
        List<RyPurchaseOut> list = ryPurchaseOutService.selectRyPurchaseOutList(ryPurchaseOut);
        ExcelUtil<RyPurchaseOut> util = new ExcelUtil<RyPurchaseOut>(RyPurchaseOut.class);
        util.exportExcel(response, list, "采购出库数据");
    }

    /**
     * 获取采购出库详细信息
     */
    @PreAuthorize("@ss.hasPermi('purchase:ryPurchaseOut:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ryPurchaseOutService.selectRyPurchaseOutById(id));
    }

    /**
     * 新增采购出库
     */
    @PreAuthorize("@ss.hasPermi('purchase:ryPurchaseOut:add')")
    @Log(title = "采购出库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RyPurchaseOut ryPurchaseOut)
    {
        return toAjax(ryPurchaseOutService.insertRyPurchaseOut(ryPurchaseOut));
    }

    /**
     * 修改采购出库
     */
    @PreAuthorize("@ss.hasPermi('purchase:ryPurchaseOut:edit')")
    @Log(title = "采购出库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RyPurchaseOut ryPurchaseOut)
    {
        return toAjax(ryPurchaseOutService.updateRyPurchaseOut(ryPurchaseOut));
    }

    /**
     * 删除采购出库
     */
    @PreAuthorize("@ss.hasPermi('purchase:ryPurchaseOut:remove')")
    @Log(title = "采购出库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ryPurchaseOutService.deleteRyPurchaseOutByIds(ids));
    }
}
